﻿using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThongTinNhanKhau.GUI
{
    public partial class ChungTu : Form
    {
        private GUI.NhanKhau nhanKhau;
        static String maNhanKhau;
        model.BO.ChungTuBO chungTuBO;

        public ChungTu(GUI.NhanKhau f, String maNK)
        {
            InitializeComponent();
            this.nhanKhau = f;
            maNhanKhau = maNK;
        }

        private void ChungTu_Load(object sender, EventArgs e)
        {
            chungTuBO = new model.BO.ChungTuBO(this, nhanKhau);
            this.tbMaNhanKhau.Text = maNhanKhau;

            chungTuBO.loadDefault(maNhanKhau);
            chungTuBO.loadTable(maNhanKhau);

            //Visible Panel Top.
            panelTop.Visible = false;
            this.gridControlChungTu.Location = new System.Drawing.Point(1, 3);
            this.gridControlChungTu.Size = new System.Drawing.Size(987, 618);
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (this.gridView1.DataRowCount == 0)
            {
                panelTop.Visible = true;
                this.gridControlChungTu.Location = new System.Drawing.Point(1, 207);
                this.gridControlChungTu.Size = new System.Drawing.Size(987, 414);

                chungTuBO.genarateMaCT();
            }
            else
            {
                MessageBox.Show("Nhân khẩu này đã tồn tại chứng tử, không thể Thêm Mới.");
            }
            
        }

        private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hitInfo = view.CalcHitInfo(e.Point);

            if (hitInfo.InRow)
            {
                view.FocusedRowHandle = hitInfo.RowHandle;
                this.contextMenuStripChungTu.Show(view.GridControl, e.Point);
            }
            else
            {
                this.contextMenuStripChungTu.Show(view.GridControl, e.Point);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            chungTuBO.btnLuu();
        }

        private void sửaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridView1.SelectedRowsCount > 0)
            {
                panelTop.Visible = true;
                this.gridControlChungTu.Location = new System.Drawing.Point(1, 207);
                this.gridControlChungTu.Size = new System.Drawing.Size(987, 414);

                tbMaChungTu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MaChungTu").ToString();
                dateTimeNgayChet.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NgayChet").ToString();
                tbNoiChet.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NoiChet").ToString();
                tbNguyenNhan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NguyenNhan").ToString();
                tbNTTCuoiCung.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NoiThuongTruCuoiCung").ToString();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            //Visible Panel Top.
            panelTop.Visible = false;
            this.gridControlChungTu.Location = new System.Drawing.Point(1, 3);
            this.gridControlChungTu.Size = new System.Drawing.Size(987, 618);
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridView1.SelectedRowsCount > 0)
            {
                String maCT = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MaChungTu").ToString();
                String maNhanKhau = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MaNhanKhau").ToString();

                DialogResult result = MessageBox.Show("Bạn có muốn xóa Mã Tạm Trú: " + maCT + "???", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                //If Yes
                if (result == DialogResult.Yes)
                {
                    chungTuBO.deleteRow(maCT, maNhanKhau);
                }
            }
        }
    }
}
